<?php
/**
 * storage items in a static class
 */

namespace shine\storage;

use ArrayIterator;

/**
 * @package storage
 * @author lixin_gg@126.com
 * @static
 */
class StaticStorage
{
    /**
     * storage all data
     * @var array
     */
    protected static $data = [];

    /**
     * get item by given key
     * @param mixed $key the key storaged before
     * @return mixed|null got the storaged item or null when the key not exists
     */
    public static function get($key)
    {
        return static::$data[$key] ?? null;
    }

    /**
     * set item by with key and the value
     * @param mixed $key storage key
     * @param mixed $item value for storage
     * @return void
     */
    public static function set($key, $item)
    {
        static::$data[$key] = $item;
    }

    /**
     * whether contains the key
     * @param mixed $key
     * @return bool
     */
    public static function contains($key): bool
    {
        return isset(static::$data[$key]);
    }

    /**
     * unset the item by key
     * @param mixed $key
     * @return void
     */
    public static function unset($key)
    {
        unset(static::$data[$key]);
    }

    /**
     * count data
     * @return int
     */
    public static function count(): int
    {
        return count(static::$data);
    }

    /**
     * get array iterator instance
     * @return ArrayIterator
     */
    public static function getArrayIterator(): ArrayIterator
    {
        return new ArrayIterator(static::$data);
    }
}
